package com.cxyd.modules.pda.controller;

import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.exception.ErrorCode;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.modules.wms.dto.*;
import com.cxyd.modules.wms.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("keda-rf")
public class RfController {
    private final PurchaseService purchaseService;
    private final PurchaseDetailService purchaseDetailService;
    private final TaskService taskService;
    private final InterfaceSapService interfaceSapService;
    private final InterfaceSapDetailService interfaceSapDetailService;

    public RfController(PurchaseService purchaseService, PurchaseDetailService purchaseDetailService, TaskService taskService, InterfaceSapService interfaceSapService, InterfaceSapDetailService interfaceSapDetailService) {
        this.purchaseService = purchaseService;
        this.purchaseDetailService = purchaseDetailService;
        this.taskService = taskService;
        this.interfaceSapService = interfaceSapService;
        this.interfaceSapDetailService = interfaceSapDetailService;
    }

    /**
     * 主单号获取明细
     */
    @GetMapping("detail-page")
    public Result<PageData<PurchaseDetailDTO>> detailPage(@RequestParam Map<String, Object> params) {
        PageData<PurchaseDetailDTO> page = purchaseDetailService.rfPage(params);

        return new Result<PageData<PurchaseDetailDTO>>().ok(page);
    }

    @GetMapping("deliver-page")
    public Result<PageData<PurchaseDetailDTO>> deliverPage(@RequestParam Map<String, Object> params) {
        final String deliverNo = (String) params.get("deliverNo");
        final String lineNo = (String) params.get("lineNo");
        if (StringUtils.isBlank(deliverNo)) {
            return new Result<PageData<PurchaseDetailDTO>>().error(ErrorCode.IDENTIFIER_NOT_NULL, "交货单号不能为空!");
        }
        //1.从wms系统获取一次
        PageData<PurchaseDetailDTO> page = purchaseDetailService.rfPage(params);
        if (page == null) {
            //2.获取不到则请求sap采购单接口
            final InterfaceInputDTO dto = new InterfaceInputDTO();
            dto.setSrmdn(deliverNo);
            dto.setUname("yeqh");
            final String url = "http://s4prd3.kedachina.com.cn:8000";
            //3.写入wms系统
            page = purchaseDetailService.rfPage(params);
        }
        return new Result<PageData<PurchaseDetailDTO>>().ok(page);
    }

    /**
     * 任务信息
     */
    @GetMapping("task-page")
    public Result<PageData<TaskDTO>> taskPage(@RequestParam Map<String, Object> params) {
        PageData<TaskDTO> page = taskService.rfPage(params);

        return new Result<PageData<TaskDTO>>().ok(page);
    }

    /**
     * 登记
     */
    @PostMapping("register")
    @LogOperation("入库登记")
    public Result register(@RequestBody Map<String, Object> params) {
        final ProcResult result = purchaseService.purchaseRegister(params);
        //处理结果
        if (!result.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, result.getMsg());
        } else
            return new Result().ok(result.getMsg());
    }

    /**
     * 余料回库
     */
    @PostMapping("residual")
    @LogOperation("余料回库")
    public Result residual(@RequestBody Map<String, Object> params) {
        ProcResult result = purchaseService.residualBackRegister(params);
        //处理结果
        if (!result.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, result.getMsg());
        } else
            return new Result().ok(result.getMsg());
    }

    /**
     * 任务确认
     */
    @PostMapping("affirm")
    @LogOperation("任务确认")
    public Result affirm(@RequestBody Map<String, Object> params) {
        final ProcResult result = taskService.taskAffirm(params);
        //处理结果
        if (!result.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, result.getMsg());
        } else
            return new Result().ok(result.getMsg());
    }

    @PostMapping("check-affirm")
    @LogOperation("盘点")
    public Result checkAffirm(@RequestBody Map<String, Object> params) {
        final ProcResult result = taskService.checkAffirm(params);
        //处理结果
        if (!result.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, result.getMsg());
        } else
            return new Result().ok(result.getMsg());
    }

    @PostMapping("empty-register")
    @LogOperation("空框登记")
    public Result emptyRegister(@RequestBody Map<String, Object> params) {
        ProcResult result = taskService.emptyRegister(params);
        //处理结果
        if (!result.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, result.getMsg());
        } else
            return new Result().ok(result.getMsg());
    }

    @PostMapping("empty-call")
    @LogOperation("空框呼叫")
    public Result emptyCall(@RequestBody Map<String, Object> params) {
        ProcResult result = taskService.emptyCall(params);
        //处理结果
        if (!result.getCode().equals("0")) {
            return new Result().error(ErrorCode.PROC_SERVE_ERROR, result.getMsg());
        } else
            return new Result().ok(result.getMsg());
    }
}
